'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    const now = new Date();
    
    // 添加用户表配置
    await queryInterface.bulkInsert('table_configs', [{
      id: Sequelize.literal('UUID()'),
      code: 'users',
      name: '用户管理',
      description: '系统用户信息管理',
      table_name: 'users',
      api_path: '/users',
      fields: JSON.stringify([
        {
          key: 'id',
          label: 'ID',
          type: 'text',
          table: {
            hidden: true,
            width: 200,
            align: 'left',
            fixed: false,
            sortable: false,
            ellipsis: false
          },
          search: {
            searchable: false
          },
          form: {
            creatable: false,
            editable: false,
            updatable: false
          }
        },
        {
          key: 'avatar',
          label: '头像',
          type: 'image',
          table: {
            width: 100,
            align: 'center',
            fixed: false,
            hidden: false,
            sortable: false,
            ellipsis: false
          },
          search: {
            searchable: false
          },
          form: {
            creatable: false,
            editable: true,
            updatable: true,
            placeholder: '请上传头像'
          }
        },
        {
          key: 'email',
          label: '邮箱',
          type: 'text',
          table: {
            width: 200,
            align: 'left',
            fixed: false,
            hidden: false,
            sortable: true,
            ellipsis: true
          },
          search: {
            searchable: true,
            searchType: 'like',
            quickSearch: true
          },
          form: {
            creatable: true,
            editable: true,
            updatable: true,
            required: true,
            rules: [
              { required: true, message: '请输入邮箱' },
              { type: 'email', message: '请输入有效的邮箱地址' }
            ],
            placeholder: '请输入邮箱'
          }
        },
        {
          key: 'password',
          label: '密码',
          type: 'text',
          table: {
            hidden: true
          },
          search: {
            searchable: false
          },
          form: {
            creatable: true,
            editable: false,
            updatable: false,
            required: true,
            rules: [
              { required: true, message: '请输入密码' },
              { min: 6, message: '密码长度不能少于6位' }
            ],
            placeholder: '请输入初始密码',
            inputType: 'password'
          }
        },
        {
          key: 'firstName',
          label: '名字',
          type: 'text',
          table: {
            width: 150,
            align: 'left',
            fixed: false,
            hidden: false,
            sortable: true,
            ellipsis: false
          },
          search: {
            searchable: true,
            searchType: 'like',
            quickSearch: false
          },
          form: {
            creatable: true,
            editable: true,
            updatable: true,
            required: false,
            placeholder: '请输入名字'
          }
        },
        {
          key: 'lastName',
          label: '姓氏',
          type: 'text',
          table: {
            width: 150,
            align: 'left',
            fixed: false,
            hidden: false,
            sortable: true,
            ellipsis: false
          },
          search: {
            searchable: true,
            searchType: 'like',
            quickSearch: false
          },
          form: {
            creatable: true,
            editable: true,
            updatable: true,
            required: false,
            placeholder: '请输入姓氏'
          }
        },
        {
          key: 'name',
          label: '姓名',
          type: 'text',
          table: {
            width: 150,
            align: 'left',
            fixed: false,
            hidden: false,
            sortable: false,
            ellipsis: true,
            // 这是一个组合字段，在渲染时会使用 firstName + lastName
            render: 'fullName'
          },
          search: {
            searchable: false
          },
          form: {
            creatable: false,
            editable: false,
            updatable: false
          }
        },
        {
          key: 'roleId',
          label: '角色',
          type: 'select',
          table: {
            width: 200,
            align: 'left',
            fixed: false,
            hidden: false,
            sortable: false,
            ellipsis: true,
            // 显示关联对象的名称
            render: 'relation',
            relationField: 'role.name'
          },
          search: {
            searchable: true,
            searchType: 'equal',
            quickSearch: false
          },
          form: {
            creatable: true,
            editable: true,
            updatable: true,
            required: true,
            rules: [{ required: true, message: '请选择角色' }],
            placeholder: '请选择角色',
            // 需要从 API 获取选项
            optionsSource: 'api',
            optionsApi: '/roles',
            optionsLabelField: 'name',
            optionsValueField: 'id'
          }
        },
        {
          key: 'departmentId',
          label: '部门',
          type: 'select',
          table: {
            width: 150,
            align: 'left',
            fixed: false,
            hidden: false,
            sortable: false,
            ellipsis: true,
            render: 'relation',
            relationField: 'department.name'
          },
          search: {
            searchable: true,
            searchType: 'equal',
            quickSearch: false
          },
          form: {
            creatable: true,
            editable: true,
            updatable: true,
            required: false,
            placeholder: '请选择部门',
            optionsSource: 'api',
            optionsApi: '/departments',
            optionsLabelField: 'name',
            optionsValueField: 'id'
          }
        },
        {
          key: 'status',
          label: '状态',
          type: 'select',
          table: {
            width: 100,
            align: 'center',
            fixed: false,
            hidden: false,
            sortable: true,
            ellipsis: false,
            // 使用标签渲染
            render: 'tag',
            tagColorMap: {
              'active': 'green',
              'inactive': 'red'
            },
            tagLabelMap: {
              'active': '启用',
              'inactive': '禁用'
            }
          },
          search: {
            searchable: true,
            searchType: 'equal',
            quickSearch: false
          },
          form: {
            creatable: true,
            editable: true,
            updatable: true,
            required: false,
            placeholder: '请选择状态',
            options: [
              { label: '启用', value: 'active' },
              { label: '禁用', value: 'inactive' }
            ],
            defaultValue: 'active'
          }
        },
        {
          key: 'lastLoginIp',
          label: '登录IP',
          type: 'text',
          table: {
            width: 150,
            align: 'left',
            fixed: false,
            hidden: false,
            sortable: false,
            ellipsis: false
          },
          search: {
            searchable: false
          },
          form: {
            creatable: false,
            editable: false,
            updatable: false
          }
        },
        {
          key: 'lastLoginAt',
          label: '登录时间',
          type: 'datetime',
          table: {
            width: 180,
            align: 'left',
            fixed: false,
            hidden: false,
            sortable: true,
            ellipsis: false,
            format: 'YYYY-MM-DD HH:mm:ss'
          },
          search: {
            searchable: false
          },
          form: {
            creatable: false,
            editable: false,
            updatable: false
          }
        },
        {
          key: 'createdAt',
          label: '创建时间',
          type: 'datetime',
          table: {
            width: 180,
            align: 'left',
            fixed: false,
            hidden: false,
            sortable: true,
            ellipsis: false,
            format: 'YYYY-MM-DD HH:mm:ss'
          },
          search: {
            searchable: false
          },
          form: {
            creatable: false,
            editable: false,
            updatable: false
          }
        },
        {
          key: 'updatedAt',
          label: '更新时间',
          type: 'datetime',
          table: {
            width: 180,
            align: 'left',
            fixed: false,
            hidden: true,
            sortable: true,
            ellipsis: false,
            format: 'YYYY-MM-DD HH:mm:ss'
          },
          search: {
            searchable: false
          },
          form: {
            creatable: false,
            editable: false,
            updatable: false
          }
        }
      ]),
      actions: JSON.stringify({
        create: true,
        update: true,
        delete: true,
        batchDelete: true,
        export: true,
        import: false
      }),
      table_props: JSON.stringify({
        size: 'middle',
        rowKey: 'id',
        bordered: false,
        pagination: {
          pageSize: 10,
          showSizeChanger: true,
          showQuickJumper: true
        },
        rowSelection: true,
        scroll: { x: 1500 }
      }),
      permissions: JSON.stringify({
        view: 'user:read',
        create: 'user:create',
        update: 'user:update',
        delete: 'user:delete'
      }),
      status: true,
      created_by: null,
      updated_by: null,
      created_at: now,
      updated_at: now
    }], {});
  },

  down: async (queryInterface, Sequelize) => {
    // 删除用户表配置
    await queryInterface.bulkDelete('table_configs', {
      code: 'users'
    }, {});
  }
};
